<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class AdController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = DB::table('carousel')->get();
//        dd($data);
        return view('/admin/ad/index',['data'=>$data]);


    }

    /**
     * Show the form for creating a new resource.
     *公示广告
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
       return view('admin/ad/create');
    }

    /**
     * Store a newly created resource in storage.
     *列表页
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //使用框架自带的验证
        $this->validate($request, [
            'name' => 'required|max:30',
            'link' => 'required',
            'ad_pic' => 'required',
        ], [
            'name.required' => '广告名称不能为空',
            'name.max' => '广告名长度不能大于30',
            'pic.required' => '广告图片不能为空',
            'link.required' => '链接地址不能为空',
        ]);
        //验证完成后去除数组中的_token
        $data = $request->all();
        unset($data['_token']);
        //如果上传了 使用处理后的图片，如果没有上传图片使用默认图片
        if ($request->hasFile('ad_pic')) {
            if ($request->file('ad_pic')->isValid()) {
                $suffix = $request->file('ad_pic')->getClientOriginalExtension();
                $fileName = time() . mt_rand(100000, 999999) . '.' . $suffix;
                $path = './home/images';
                $request->file('ad_pic')->move($path, $fileName);
                $data['ad_pic'] = trim($path . '/' . $fileName, '.');
            }
        }
//        //执行插入
        $res = DB::table('carousel')->insert($data);
        //对结果并进行判断并跳转到列表页
        if($res)
        {
            return redirect('/admin/ad') -> with('success','添加成功');
        }else
        {
            return back() -> with('error','添加失败');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *编辑页
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //编辑页面
        $data = DB::table('carousel') -> where('car_id','=',$id) -> first();
//        dd($data);
        return view('/admin/ad/edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //更新
        //使用框架自带的验证
        $this->validate($request, [
            'name' => 'required|max:30',
            'link' => 'required',

        ], [
            'name.required' => '广告名称不能为空',
            'name.max' => '广告名长度不能大于30',
            'link.required' => '链接地址不能为空',
        ]);
        //验证完成后去除数组中的_token
        $data = $request->all();
        unset($data['_token']);
        //如果上传了 使用处理后的图片，如果没有上传图片使用默认图片
        if ($request->hasFile('ad_pic')) {
            if ($request->file('ad_pic')->isValid()) {
                $suffix = $request->file('ad_pic')->getClientOriginalExtension();
                $fileName = time() . mt_rand(100000, 999999) . '.' . $suffix;
                $path = './uploads';
                $request->file('ad_pic')->move($path, $fileName);
                $data['ad_pic'] = trim($path . '/' . $fileName, '.');
            }
        }else{
            $data['ad_pic'] = $data['oldpic'];
        }
        unset($data['oldpic']);
        unset($data['_method']);
        unset($data['id']);
//        //执行插入
        $res = DB::table('carousel')->where('car_id',$id)-> update($data);
        //对结果并进行判断并跳转到列表页
        if($res)
        {
            return redirect('/admin/ad') -> with('success','更新成功');
        }else
        {
            return back() -> with('error','更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除
        $res=DB::table('carousel') -> where('car_id','=',$id)->delete();
        if($res)
        {
            return back() -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }
}
